package com.sport.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sport.bean.Admin;
import com.sport.bean.PageParam;
import com.sport.bean.PageResult;
import com.sport.service.IAdminService;
import com.sport.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author wxq
 * @Date 2021/5/8
 **/

@Controller
public class AdminController {
    @Autowired
    private IAdminService adminService;


    /**
        分页查询管理员
     */
    @GetMapping(value = "/admin/list")
    @ResponseBody
    public PageResult<Admin> findAll1(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                      HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        PageParam<Admin> pageParam = new PageParam<>(request);
        List<Admin> admins = adminService.listPage(pageParam);
        PageInfo info = new PageInfo(admins);
        return new PageResult<Admin>(admins, info.getTotal());
    }

    /**
        删除
     */
    @GetMapping("/admin/del")
    @ResponseBody
    public JsonResult delAdmin(Integer id) {
        if (adminService.deleteAdmin(id) > 0) {
            return JsonResult.ok("删除成功");
        } else {
            return JsonResult.error("删除失败");
        }
    }

    /**
   id查找
    */
    @GetMapping("/admin/findId")
    @ResponseBody
    public Admin findId(Integer id) {
        return adminService.findById(id);
    }

    /**
        修改admin
     */
    @GetMapping("/admin/update")
    @ResponseBody
    public JsonResult updatAdmin(MultipartFile file, Admin admin) {
        if ( adminService.updateInfo(admin) > 0) {
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改失败");
        }

    }

    /**
     添加admin
   */
    @GetMapping("/admin/add")
    @ResponseBody
    public JsonResult addAdmin(MultipartFile file, Admin admin) {

        admin.setLevel(2);//级别
        if ( adminService.addAdmin(admin) > 0) {
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }

    }

    /**
     添加admin
   */
    @GetMapping("/admin/findByname")
    @ResponseBody
    public JsonResult findByname(String username) {
      if (adminService.findByName(username)!=null){
          return JsonResult.error("用户名已存在");
      }else {
          return null;
      }
    }


    /**
        登录账号资料显示
     */
    @GetMapping("/admin/personalData")
    @ResponseBody
    public Admin personalData(HttpSession session) {
        Admin admin=(Admin)session.getAttribute("admin");
        return admin;
    }

    /**
     * 退出登陆
     */
    @GetMapping("/admin/logout")
    public void logout(HttpSession session, HttpServletResponse response) throws IOException {
        session.removeAttribute("admin");
        response.sendRedirect("/sport/");
    }

//    public void eiit()


}